<template>
	<view class="img-information" :animation="animationData" > 
	<!-- :style="{bottom: isShow ? '-760upx' : '0'}" -->
		<view class="line-show" @click="show">
			<view class="line"></view>
		</view>
		<view class="information">
			<view class="author-information">
				<view class="left">
					<image :src="userInfo.avator" mode=""></image>
					<text>{{userInfo.nickname}}</text>
				</view>
				<view class="right">
					他的作品
				</view>
			</view>
			<view class="img-introduce">
				<view class="title-tag">
					<text>{{opus.title}}</text>
					<view class="tag-list">
						<view class="tag" v-for="(tag, index) in tagList" :key="index">#{{tag}}</view>
					</view>
				</view>
				<text>{{opus.description}}</text>
			</view>
			<view class="down-share-like">
				<view class="btn">
					<image :src="downloadIon" mode=""></image>
					<text>{{opus.downloadNum}}</text>
				</view>
				<view class="btn">
					<image :src="likeIcon" mode=""></image>
					<text>{{opus.likeNum}}</text>
				</view>
				<view class="btn">
					<image :src="shareIcon" mode=""></image>
					<text>{{opus.shareNum}}</text>
				</view>
			</view>
			<view class="down-like">
				<view class="image">
					<image :src="likeIcon" mode=""></image>
				</view>
				<button class="btn" @click="download(opus.img)">{{progress !== 0? `正在下载${progress}%` : '下载原图' }}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {download_a_icon, like_icon, share_icon} from '@/static/svg/index.js'
	import { getDomHeight } from '@/common/utils/index.js'
	import { downLoad } from '@/common/api/index.js'
	let downloadTask = null
	export default {
		name: 'ImgInformation',
		props: {
			opus: {
				type: Object,
				default: {}
			},
			userInfo: {
				type: Object,
				default: {}
			}
		},
		data () {
			return {
				smoothHeight: 0,
				progress: 0,
				animationData: {},
				isShow: true,
				downloadIon: download_a_icon,
				likeIcon: like_icon,
				shareIcon: share_icon
			}
		},
		computed: {
		   tagList: function() {
			   let tag = this.opus.tags
			   let tagList = tag.split(',')
			   return tagList
		   }
		},
		onShow() {
			this.ani()
		},
		mounted() {
			getDomHeight('img-information').then(res => {
				console.log(res,'domxixni')
				this.smoothHeight = res.bottom * 0.4
			})
		},
		methods: {
			async downIt() {
				let res = await downLoad(this.opus.id)
				if (res.code === 0){
					console.log('上传下载信息成功')
				}
			},
			show () {
				this.ani()
				this.isShow = !this.isShow
			},
			download (url) {
				downloadTask = uni.downloadFile({
				    url: url, //仅为示例，并非真实的资源
				    success: (res) => {
				        if (res.statusCode === 200) {
							this.downIt()
				            console.log('下载成功',res.tempFilePath);
							  uni.saveImageToPhotosAlbum({
							            filePath: res.tempFilePath,
							            success: function () {
							                console.log('save success');
							            },
										fail:function(err){
											console.log('下载失败了')
										}
							  });
				        }
				    }
				});
				downloadTask.onProgressUpdate((res) => {
				    console.log('下载进度' + res.progress);
					this.progress = res.progress
					if (res.progress == 100) {
						setTimeout(() => {
							this.progress = 0
						},500)
					}
				    console.log('已经下载的数据长度' + res.totalBytesWritten);
				    console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				});
			},
			ani () {
				var animation = uni.createAnimation({
					duration: 600,
					timingFunction: 'ease-in',
				})
				this.animation = animation
				let ani = null
				if ( !this.isShow ) {
					animation.translateY(this.smoothHeight).step()
					ani = animation.export()
				} else {
					animation.translateY(0).step()
					ani = animation.export()
				}
				// animation.scale(0.5).step()
				
				this.animationData = ani
			}
		}
	}
</script>

<style lang="less" scoped>
	.img-information {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		padding-bottom: 170upx;
		padding-top: 44upx;
		box-sizing: border-box;
		background-color: #FFFFFF;
		border-radius: 80upx 80upx 0 0;
		color: rgba(16, 16, 16, 100);
		font-size: 28upx;
		.line-show {
			width: 100%;
			padding-bottom: 128upx;
			//background-color: red;
			display: flex;
			flex-direction: row;
			justify-content: center;
			.line {
				width: 60upx;
				height: 8upx;
				line-height: 40upx;
				border-radius: 100upx;
				background-color: rgba(221, 221, 221, 100);
				text-align: center;
				border: 2upx solid rgba(255, 255, 255, 100);
			}
		}
		.information {
			width: 100%;
			padding: 0 60upx;
			box-sizing: border-box;
			.author-information {
				width: 100%;
				margin-bottom: 60upx;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				.left {
					display: flex;
					flex-direction: row;
					align-items: center;
					image {
						width: 100upx;
						height: 100upx;
						border-radius: 50%;
						margin-right: 32upx;
					}
					text {
						color: rgba(16, 16, 16, 100);
						font-size: 32upx;
					}
				}
				.right {
					width: 164upx;
					height: 64upx;
					border-radius: 32upx;
					background-color: rgba(244, 244, 244, 100);
					// color: rgba(245, 245, 245, 100);
					color: rgba(16, 16, 16, 100);
					font-size: 24upx;
					line-height: 64upx;
					text-align: center;
				}
			}
		}
		.img-introduce {
			width: 100%;
			display: flex;
			flex-direction: column;
			.title-tag {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				padding-bottom: 20upx;
				text {
					color: rgba(16, 16, 16, 100);
					font-size: 46upx;
					text-align: left;
				}
				.tag-list {
					display: flex;
					flex-direction: row;
					.tag {
						height: 44upx;
						padding: 0 20upx;
						border-radius: 22upx;
						line-height: 44upx;
						background-color: rgba(244, 244, 244, 100);
						color: rgba(208, 208, 208, 100);
						font-size: 22upx;
						text-align: center;
						border: 2upx solid rgba(255, 255, 255, 100);
						margin-left: 20upx;
					}
					
				}
			}
			text {
				color: rgba(16, 16, 16, 100);
				font-size: 28upx;
				text-align: justify;
			}
		}
		.down-share-like {
			width: 100%;
			margin: 40upx 0;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			.btn {
				display: flex;
				flex-direction: row;
				align-items: center;
				image {
					width: 36upx;
					height: 36upx;
					margin-right: 20upx;
				}
				text {
					color: rgba(16, 16, 16, 100);
					font-size: 24upx;
					text-align: left;
				}
			}
		}
		.down-like {
			position: absolute;
			bottom: 68upx;
			margin: 0 auto;
			width: 630upx;
			height: 108upx;
			border-radius: 54upx;	
			background-color: rgba(16, 16, 16, 100);
			color: rgba(16, 16, 16, 100);
			box-sizing: border-box;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			.image {
				width: 128upx;
				height: 100%;
				border-radius:  54upx 0 0 54upx;
				background-color: rgba(45, 45, 45, 100);
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				image {
					margin: 0 auto;
					width: 48upx;
					height: 48upx;
				}
			}
			.btn {
				flex: 1;
				width: 100%;
				height: 100%;
				border-radius:  0 54upx 54upx 0;
				background-color: rgba(16, 16, 16, 100);
				//background-color: red;
				line-height: 108upx;
				text-align: center;
				color: #FFFFFF
			}
		}
	}
</style>
